// pages/mycomments/mycomments.js

var globalData = getApp().globalData;
var custId = globalData.custID;
var Status = 0;
var PageSize = 3;
var PageIndex = 1;
var common = require("../../utils/Conmon.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    popshow: false,
    textshow:false,
    textbtn:"回复",
    textput:"提交",

    IsLastPage: false,
    MyCommentList: {},
    Popup_GoodsImg: '',
    Popup_ProductName: '',
    Popup_SpecValue: '',
    Popup_Price: '',
    Popup_CommentID: 0,
    Popup_Content: '',
    Popup_BeReplyUserID: 0,
    Popup_Placeholder: ''
  },

  /**
   * 填写回复内容
   */
  onContentInput: function (e) {
    this.setData({
      Popup_Content: e.detail.value
    })
  },

  openPop: function(e){
    if (e.currentTarget.dataset.bereplyuserid == wx.getStorageSync('UserInfo').UserID) {
      wx.showToast({
        title: '不能回复自己',
        duration: 2000
      })
      return false;
    }

    this.setData({
      popshow: true,
      Popup_GoodsImg: e.currentTarget.dataset.goodsimg,
      Popup_ProductName: e.currentTarget.dataset.productname,
      Popup_SpecValue: e.currentTarget.dataset.specvalue,
      Popup_Price: e.currentTarget.dataset.price,
      Popup_CommentID: e.currentTarget.dataset.commentid,
      Popup_BeReplyUserID: e.currentTarget.dataset.bereplyuserid,
      Popup_Placeholder: e.currentTarget.dataset.placeholder
    })
  },

  closePop: function () {
    if (this.data.Popup_Content == '') {
      wx.showToast({
        title: '请填写回复内容',
        duration: 2000
      })
      return false;
    }

    var that = this;
    var url = globalData.apiurl + '/mobile/api/Product/AddProductCommentReply';

    var d = new Object();
    d.CommentID = this.data.Popup_CommentID;
    d.Content = this.data.Popup_Content;
    d.ReplyUserID = wx.getStorageSync('UserInfo').UserID;
    d.BeReplyUserID = this.data.Popup_BeReplyUserID;
    d.CustID = custId;
    d.ParentID = 0;
    d.IsKeFu = 0;

    wx.showModal({
      title: '回复',
      content: '确定回复吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: url,
            data: d,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.showToast({
                title: '回复成功',
                duration: 2000
              })
              that.setData({
                popshow: false
              })                            
              // wx.navigateTo({
              //   url: '/pages/mycomments/mycomments',
              // })
            },
            fail: function () {
              wx.showToast({
                title: '回复失败',
                duration: 2000
              })
            },
            complete: function () {
              //complete
            }
          })
        }
      }
    })
  },

  showModal: function () {
    this.setData({
      textshow: !this.data.textshow
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () {
    });
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    LoadCommentList(this, PageIndex);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (PageIndex == 1) {
      LoadCommentList(this, 1);
      wx.showToast({
        title: '已到顶',
        duration: 2000
      })
      return false;
    }

    this.data.IsLastPage = false;
    LoadCommentList(this, --PageIndex);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.IsLastPage) {
      wx.showToast({
        title: '已到底',
        duration: 2000
      })
      return false;
    }
    LoadCommentList(this, ++PageIndex);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})

function LoadCommentList (obj, pageindex) {
  var url = globalData.apiurl + '/mobile/api/Product/GetCommentList';

  wx.request({
    url: url,
    data: { "CustId": custId, "Id": wx.getStorageSync('UserInfo').UserID, "Status": Status, "PageIndex": pageindex, "PageSize": PageSize, "IsMyComment": 0 },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      wx.hideLoading();
      if (res.data.content == null) {
        if (pageindex == 1) {
          wx.showToast({
            title: '无数据',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '已到底',
            duration: 2000
          })
        }
        obj.data.IsLastPage = true;
        return false;
      }
      var d = res.data.content.Data;
      for (var i = 0; i < d.length; i++) {
        if (d[i].Picture.length > 0) {
          var pics = d[i].Picture.split(',');
          for (var j = 0; j < pics.length; j++) {
            pics[j] = 'http://img4test.winmobi.cn' + pics[j];
          }
          d[i].Picture = pics;
        }
      }
      obj.setData({
        MyCommentList: d
      })
      if (d.length != PageSize) {
        if (pageindex == 1) {

        } else {
          if (d.length == 0) {
            wx.showToast({
              title: '无数据',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '已到底',
              duration: 2000
            })
          }
        }
        obj.data.IsLastPage = true;
        return false;
      }
    },
    fail: function () {
      wx.hideLoading();
      wx.showToast({
        title: '获取我的评论失败',
        duration: 2000
      })
    },
    complete: function () {
      //complete
    }
  })
}